package com.jwsoft.manager.core.dao.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson2.JSON;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.module.base.core.helper.DictHelper;
import com.bifang.module.system.core.dao.model.AppArticle;
import com.bifang.module.system.core.dao.model.AppArticleContent;
import com.bifang.module.system.core.dao.service.AppArticleContentService;
import com.bifang.module.system.core.dao.service.AppArticleService;
import com.jwsoft.manager.common.constant.EduCache;
import com.jwsoft.manager.common.enums.EnrollmenDictEnum;
import com.jwsoft.manager.common.vo.eduEnrollmenBrochure.EduEnrollmenBrochureKeyVO;
import com.jwsoft.manager.common.vo.eduEnrollmenBrochure.EnrollmenBrochureVO;
import com.jwsoft.manager.common.vo.eduEnrollmenBrochure.OtherAreaConfigNameVO;
import com.jwsoft.manager.common.vo.eduSchool.EduSchoolVO;
import com.jwsoft.manager.core.dao.model.EduEnrollmenBrochure;
import com.jwsoft.manager.core.dao.mapper.EduEnrollmenBrochureMapper;
import com.jwsoft.manager.core.dao.model.EduSchool;
import com.jwsoft.manager.core.dao.service.EduEnrollmenBrochureService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwsoft.manager.core.dao.service.EduSchoolService;
import com.jwsoft.manager.core.util.EduHelper;
import org.apache.commons.lang3.StringUtils;
import org.jooq.tools.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 各学校招生简章服务实现类
 *
 * @author chenchao
 * @since 2023-01-11
 */
@Service
public class EduEnrollmenBrochureServiceImpl extends ServiceImpl<EduEnrollmenBrochureMapper, EduEnrollmenBrochure> implements EduEnrollmenBrochureService {
    @Autowired
    private AppArticleService appArticleService;
    @Autowired
    private AppArticleContentService appArticleContentService;
    @Autowired
    private DictHelper dictHelper;
    @Autowired
    private EduHelper eduHelper;
    @Override
    public List<EnrollmenBrochureVO> getEnrollMenInfoList(Map<String, Object> map) {
        return this.baseMapper.getEnrollMenInfoList(map);
    }

    @Override
    public List<EnrollmenBrochureVO> getManEnrollMenInfoList(Map<String, Object> map) {
        return this.baseMapper.getManEnrollMenInfoList(map);
    }


    @Override
    @Cached(name = EduCache.EDU_BROCHURE_ID, key = "#vo.enrollmenBrochureId", cacheType = CacheType.BOTH, expire = 86400, localExpire = 20)
    public EnrollmenBrochureVO getDetail(EduEnrollmenBrochureKeyVO vo) {
        EduEnrollmenBrochure brochure =null;
        AppArticle appArticle=null;
        if(vo.getEnrollmenBrochureId()!=null){
            brochure = this.getById(vo.getEnrollmenBrochureId());
            if (ObjectUtils.isEmpty(brochure)) {
                throw new AppException("查询不到详细信息!");
            }
            appArticle=appArticleService.getById(brochure.getArticleId());
            if (ObjectUtils.isEmpty(appArticle)) {
                throw new AppException("查询不到内容详细信息!");
            }
            if(StringUtils.isBlank(appArticle.getArticleContent())){
                AppArticleContent appArticleContent =appArticleContentService.getById(brochure.getArticleId());
                if (appArticleContent!=null){
                    appArticle.setArticleContent(appArticleContent.getArticleContent());
                }
            }
        }else{
            throw new AppException("招生简章id不能为空");
        }

        EnrollmenBrochureVO result = Convert.convert(EnrollmenBrochureVO.class, appArticle);
        BeanUtil.copyProperties(brochure,result);
        if (StringUtils.isNotBlank(result.getAdmissionsCategory())){
            List<OtherAreaConfigNameVO> configNameVOList= JSON.parseArray(result.getAdmissionsCategory(),OtherAreaConfigNameVO.class);
            result.setEnrollAreaConfigList(configNameVOList);
        }else{
            //为空则不控制
            result.setEnrollAreaConfigList(new ArrayList<>());
        }

        dictHelper.valueToName(result, Arrays.asList(EnrollmenDictEnum.values()));
        return result;
    }

    @Override
    @Cached(name = EduCache.EDU_BROCHURE_areaCode, key = "#areaCode", cacheType = CacheType.BOTH, expire = 86400, localExpire = 20)
    public Map<String, EnrollmenBrochureVO> getThisYearBrochure(String areaCode) {
        Map<String, Object> map=new HashMap<>();
        map.put("areaCode",areaCode);
        map.put("year",eduHelper.thisYear());
        List<EnrollmenBrochureVO> list=this.getEnrollMenInfoList(map);
        if(CollectionUtils.isEmpty(list)){
            return new HashMap<>();
        }
        return list.stream().collect(Collectors.toMap(EnrollmenBrochureVO::getSchoolId, a -> a,(k1, k2)->k1));
    }
}
